<template>
    <div>
        <Row class="formRow">
          <Col span="2" class="rightRow">
            <span class="ModelInputTitle">类型编号</span>
          </Col>
          <Col span="2" class="leftRow">
            <Input v-model="formInline.typeCode" placeholder="类型编号" clearable style="width: 100%" size="large"></Input>
          </Col>
          <Col span="2" class="rightRow">
            <span class="ModelInputTitle">类型名称</span>
          </Col>
          <Col span="2" class="leftRow">
              <Input v-model="formInline.typeName" placeholder="类型名称" clearable style="width: 100%" size="large"></Input>
          </Col>
          <Col span="2" class="rightRow">
            <span class="ModelInputTitle">是否有效</span>
          </Col>
          <Col span="2" class="leftRow">
            <Select  size="large" style="width:100%" v-model="formInline.valid">
              <Option  value="1" >是</Option>
              <Option  value="0" >否</Option>
            </Select>
          </Col>
          <Col span="12" class="leftRow">
            <Button type="primary" @click="onSubmit">查询</Button>
            <Button type="primary" @click="resetForm">重置</Button>
            <Button type="primary" @click="onAdd">添加</Button>
          </Col>
        </Row>


      <Table :data="tableData" :columns="cloums" stripe :loading="loading"  style="margin-top: 20px"></Table>
      <div style="margin: 10px;overflow: hidden">
        <div style="float: right;">
          <Page :total="total" :current="1" :placement="placement" @on-change="changePage" show-total show-elevator show-sizer @on-page-size-change="changeSize"></Page>
        </div>
      </div>

      <Modal
        v-model="add"
        title="添加"
        @on-ok="addok"
        @on-cancel="cancel('您取消了操作')" width="600">
        <Row>
          <Col span="12">   <span class="ModelInputTitle"><Icon type="ios-medical" color="red"></Icon>类型编号</span><Input v-model="addForm.typeCode" style="width: 200px"></Input></Col>
          <Col span="12">   <span class="ModelInputTitle"><Icon type="ios-medical" color="red"></Icon>类型名称</span><Input v-model="addForm.typeName" style="width: 200px"></Input></Col>
        </Row>
        <Row style="margin-top: 20px">
          <Col span="12">   <span class="ModelInputTitle">是否有效</span> <Select   v-model="addForm.valid" style="width: 200px">
          <Option  value="1" >是</Option>
          <Option  value="0" >否</Option>
        </Select></Col>
          <Col span="12">   <span class="ModelInputTitle">备注</span><Input v-model="addForm.remark" style="width: 200px"></Input></Col>
        </Row>
      </Modal>


      <Modal
        v-model="edit"
        title="编辑"
        @on-ok="editok"
        @on-cancel="cancel('您取消了操作')" width="600">
        <Row>
          <Col span="12">   <span class="ModelInputTitle">类型编号</span><Input v-model="editForm.typeCode" style="width: 200px" disabled></Input></Col>
          <Col span="12">   <span class="ModelInputTitle">类型名称</span><Input v-model="editForm.typeName" style="width: 200px"></Input></Col>
        </Row>
        <Row style="margin-top: 20px">
          <Col span="12">   <span class="ModelInputTitle">是否有效</span> <Select   v-model="editForm.valid" style="width: 200px">
          <Option  value="1" >是</Option>
          <Option  value="0" >否</Option>
        </Select></Col>
          <Col span="12">   <span class="ModelInputTitle">备注</span><Input v-model="editForm.remark" style="width: 200px"></Input></Col>
        </Row>
      </Modal>

    </div>
</template>
<script type="text/ecmascript-6">
  import axios from "axios"
    export default {
      name:"SysDictType",
        data() {
            return {
              formInline:{
                valid:"",
                typeCode:"",
                typeName:""
              },
              loading:true,
              total:0,//总条数
              page:1,
              pageSize:10,
              placement:"bottom",
              add:false,//添加
              addForm:{
                typeCode:"",
                typeName:"",
                valid:"",
                remark:""
              },
              edit:false,
              editForm:{
                typeCode:"",
                typeName:"",
                valid:"",
                remark:"",
                id:""
              },
              tableData:[],
              cloums:[
                {
                  title: '类型编号',
                  key: 'typeCode',
                  width:200,
                  align:"center"
                },
                {
                  title: '类型名称',
                  key: 'typeName',
                  width:200,
                  align:"center"
                },
                {
                  title: '是否有效',
                  key: 'valid',
                  width:200,
                  align:"center"
                },
                {
                  title: '创建人',
                  key: 'creator',
                  width:200,
                  align:"center"
                },
                {
                  title: '更新人',
                  key: 'updater',
                  width:200,
                  align:"center"
                },
                {
                  title: '备注',
                  key: 'remark',
                  align:"center"
                },
                {
                  title: '操作',
                  width:200,
                  align:"center",
                  fixed:"right",
                  render:(h,params)=>{
                      return h("div",[
                        h("Button",{
                            props:{
                              type:"primary"
                            },
                          on:{
                              click:()=>{
                                console.log(params.row)
                                this.edit = true;
                                this.editForm.id = params.row.id;
                                this.editForm.typeCode = params.row.typeCode;
                                this.editForm.typeName = params.row.typeName;
                                this.editForm.remark = params.row.remark;
                                this.editForm.valid = params.row.valid=="是"?"1":"0";
                              }
                          }
                        },"编辑")
                      ])
                  }
                },
              ]
            }
        },
      mounted(){
        this.init();
      },
      methods:{
        init(page,pageSize,typeCode,typeName,valid){
          var _this = this;
            var qs = require("qs");
            var param =  qs.stringify({
              page:page?page:this.page,
              pageSize:pageSize?pageSize:this.pageSize,
              typeCode:typeCode?typeCode:"",
              typeName:typeName?typeName:"",
              valid:valid?valid:""
            });
          axios.defaults.headers.common['Authorization'] = sessionStorage.getItem("token");
          axios.post("/dictType/querySysDictTypeListByPage",param).then((res)=>{
              console.log(res.data)
            if(res.data.code=="000000"){
              _this.loading = false;
              _this.total = res.data.count;
              _this.tableData = res.data.data;
              for(var i in _this.tableData){
                _this.tableData[i].valid=_this.tableData[i].valid==1?"是":"否";
              }
            }else{
              _this.$Notice.error({title:res.data.msg})
            }
          })
        },
        onSubmit(){
          this.formInline.valid=="1"?"是":"否";
          this.init(this.page,this.pageSize,this.formInline.typeCode,this.formInline.typeName,this.formInline.valid);
        },
        resetForm(){
          this.formInline.typeName="";
          this.formInline.typeCode="";
          this.formInline.valid = "";
        },
        onAdd(){
          this.add = true;
        },
        changePage(page){
          this.loading = true;
          this.page = page;
          this.init(this.page,this.pageSize,this.formInline.typeCode,this.formInline.typeName,this.formInline.valid);
        },
        changeSize(size){
          this.placement = size>=20?"top":"bottom";
          this.loading = true;
          this.size = size;
          this.init(this.page,this.pageSize,this.formInline.typeCode,this.formInline.typeName,this.formInline.valid);
        },
        addok(){
          var qs = require("qs");
          var param = qs.stringify({
            typeCode:this.addForm.typeCode,
            typeName:this.addForm.typeName,
            valid:this.addForm.valid,
            remark:this.addForm.remark
          })
          axios.post("/dictType/addDictType",param).then((res)=>{
            if(res.data.code=="000000"){
              this.$Notice.success({title:"添加成功"});
              this.init(this.page,this.pageSize,this.formInline.typeCode,this.formInline.typeName,this.formInline.valid);
            }else{
              this.$Notice.error({title:res.data.msg});
            }
          })
        },
        editok(){
          var qs = require("qs");
          var param = qs.stringify({
            typeCode:this.editForm.typeCode,
            typeName:this.editForm.typeName,
            valid:this.editForm.valid,
            remark:this.editForm.remark,
            id:this.editForm.id
          })
          axios.post("/dictType/updateDictType",param).then((res)=>{
            if(res.data.code=="000000"){
              this.$Notice.success({title:"修改成功"});
              this.init(this.page,this.pageSize,this.formInline.typeCode,this.formInline.typeName,this.formInline.valid);
            }else{
              this.$Notice.error({title:res.data.msg});
            }
          })
        },
        cancel (msg) {
          this.$Notice.info({title:msg});
        },
      }
    }
</script>
